/**
 * author:苏恩泽
 */
/**格式化日期**/
export function formatDate (date, fmt) {
  if(date== undefined||date==null) {
	  return "";
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

//cjianquan 2019/7/12
//date之后n天，负数为前n天
//参数为空，默认返回当前时间
export function addDates(_date,_interval){
    var now = new Date();
    if(!_date){
        return now;
    }
    if(!_interval){
        _interval = 0;
    }

    var resultTime = _date.getTime()+(_interval*60*60*24*1000);
    return new Date(resultTime);
}

//cjianquan 2019/7/12
//date之后n个月，负数为前n个月
//参数为空，默认返回当前时间
export function addMonths(_date,_interval){
    if(!_date){
        return new Date();
    }
    if(!_interval){
        _interval = 0;
    }
    var month = _date.getMonth() + _interval;
    var rtn = new Date(_date.getFullYear(),month,1);
    return rtn;
}

function padLeftZero (str) {
  return ('00' + str).substr(str.length)
}

/**几天前**/
export function daysBefore(date){
	 if(date== undefined||date==null) {
		  return "";
	 }
	 var now = new Date();
     var lastVisitTime = new Date(date);
     var days = parseInt(Math.abs(now - lastVisitTime) / 1000 / 60 / 60 / 24);
     return days;
}


/**格式化 金额**/
export function formatMoney(s) {
/* 	//金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56
	var str = (val / 100).toFixed(2) + '';
	var intSum = str.substring(0, str.indexOf(".")).replace(/\B(?=(?:\d{3})+$)/g, ','); //取到整数部分
	var dot = str.substring(str.length, str.indexOf(".")) //取到小数部分搜索
	var ret = intSum + dot;
	return "￥" + ret; */
	 if (/[^(\-|\+?)0-9\.]/.test(s))
     return "0.00";
     var negative  ='';
     if(s<0)
     {negative  ='-';
         s=Math.abs(s); 
        }

     if (s == null || s == "null" || s == "")
         return "￥"+"0.00";
     s = s.toString().replace(/^(\d*)$/, "$1.");
     s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
     s = s.replace(".", ",");
     var re = /(\d)(\d{3},)/;
    while (re.test(s))
         s = s.replace(re, "$1,$2");
     s = s.replace(/,(\d\d)$/, ".$1");
   
    /*     var a = s.split(".");
         if (a[1] == "00") {
             s = a[0];
         } */
    
     return "￥"+negative+s;
}
